
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/*
 * Created on May 4, 2005
 */

/**
 * @author Jesse Phillips
 */
public class smallRPG extends JFrame {
	int playerHP = 10;
	int playerXP = 0;
	int playerAttack;
	int playerDef;
	int playerForce = 20;
	
	int cpuHP = 5;
	int cpuXP = 0;
	int cpuForce = 10;
	Random rnd = new Random();
	JTextField playerName = new JTextField();
	
    JLabel health = new JLabel();
    JLabel hpLabel = new JLabel();
    JLabel xp = new JLabel();
    JLabel xpLabel = new JLabel();
    JLabel totalForce = new JLabel();
    JLabel tfLabel = new JLabel();
    JTextField attackForce = new JTextField();
    JLabel aForce = new JLabel();
    JTextField defenceForce = new JTextField();
    JLabel dForce = new JLabel();
    
    JLabel cpuLabel = new JLabel();
    JLabel cpuHealth = new JLabel();
    
    JButton battle = new JButton();
    JButton updateButton = new JButton();
    
    JTextField output = new JTextField();
    
    JLabel spacer = new JLabel();
    
    public smallRPG() {
    	setTitle("Smally RPG Game");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                exitForm(e);
            }
        });
        getContentPane().setLayout(new GridBagLayout());

        int x = 0;
        int y = 0;
        GridBagConstraints gridConstraints = new GridBagConstraints();
        
        spacer.setText("");
        
        updateButton.setText("Update");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(updateButton, gridConstraints);
        
        hpLabel.setText("Health: ");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(hpLabel, gridConstraints);
        
        xpLabel.setText("Experiance: ");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(xpLabel, gridConstraints);
        
        aForce.setText("Attack Force: ");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(aForce, gridConstraints);
        
        dForce.setText("Defence Force: ");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(dForce, gridConstraints);
        
        tfLabel.setText("Force Left: ");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(tfLabel, gridConstraints);
        
        y = 0;
        x++;
        
        playerName.setText("Enter Your Name!");
        playerName.setColumns(10);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(playerName, gridConstraints);
        
        health.setText(""+playerHP);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(health, gridConstraints);
        
        xp.setText(""+playerXP);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(xp, gridConstraints);
        
        attackForce.setText("");
        attackForce.setColumns(10);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(attackForce, gridConstraints);
        
        defenceForce.setText("");
        defenceForce.setColumns(10);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(defenceForce, gridConstraints);
       
        totalForce.setText(""+playerForce);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(totalForce, gridConstraints);
        
        output.setText("");
        output.setColumns(13);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(output, gridConstraints);
        
        y = 0;
        x++;
        cpuLabel.setText("Computer: ");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(cpuLabel, gridConstraints);
        
        cpuHealth.setText(""+cpuHP);
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(cpuHealth, gridConstraints);
        
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(spacer, gridConstraints);
        
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(spacer, gridConstraints);
        
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        getContentPane().add(spacer, gridConstraints);
        
        battle.setText("Fight!!");
        gridConstraints.gridx = x;
        gridConstraints.gridy = y++;
        battle.setEnabled(false);
        getContentPane().add(battle, gridConstraints);
        battle.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fight();
            }
        });
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
        pack();
    }
    private void fight() {
/*ans bl*/
    	battle.setEnabled(false);
    	int cpuAttack = rnd.nextInt(cpuForce+1);
    	int cpuDef = cpuForce - cpuAttack;
    	
    	int damage = playerDef - cpuAttack;
    	if(damage < 0) {
    		playerHP += damage;
    		output.setText("You took "+damage+" damage.");
    		health.setText(""+playerHP);
    	}else playerDef = damage;
    	int cpuDamage = cpuDef - playerAttack;
    	if(cpuDamage < 0) {
    		cpuHP += cpuDamage;
    		output.setText("Computer took "+cpuDamage+" damage.");
    		cpuHealth.setText(""+cpuHP);
    	}else cpuDef = cpuDamage;
    	if(cpuHP < 0) {
    		output.setText("Computer was killed.");
    	}
//    	System.out.println("playerHP: "+playerHP);
//    	System.out.println("playerDamage: "+damage);
//    	System.out.println("cpuHP: "+cpuHP);
//    	System.out.println("cpuDamage: "+cpuDamage);
/*ans ab*/
    }
    private void update() {
    	playerAttack = Integer.valueOf(attackForce.getText()).intValue();
    	playerDef = Integer.valueOf(defenceForce.getText()).intValue();
    	if(playerAttack+playerDef > playerForce) output.setText("Force Exceeds Total.");
    	else {
    		battle.setEnabled(true);
    		if(playerAttack+playerDef == playerForce) {
    			totalForce.setText("0");
    			output.setText("You are ready to fight.");
    		}else {
    			int left = playerForce - playerAttack - playerDef;
    			totalForce.setText(""+left);
    			output.setText("Increase Att. or Def.");
    		}
    	}
    }
    private void exitForm(WindowEvent e) {
        System.exit(0);
    }
	public static void main(String[] args) {
		new smallRPG().show();
	}

}
